package com.kpmg.datalake.accountcheck.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kpmg.datalake.accountcheck.service.AccountCheckService;
import com.kpmg.datalake.schedule.enums.ScheduleParamEnum;
import com.kpmg.datalake.schedule.model.ScheduleJob;
import com.kpmg.datalake.schedule.service.ScheduleExecuteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountCheckJobImpl implements ScheduleExecuteService {

  private static final Logger logger = LoggerFactory.getLogger(AccountCheckJobImpl.class);

  @Autowired
  AccountCheckService accountCheckService;

  @Override
  public void execute(ScheduleJob scheduleJob) {
    String jobName = scheduleJob.getJobName();
    try {
      String jobParam = scheduleJob.getJobParam();
      JSONObject jsonObject = JSONObject.parseObject(jobParam);
      String projectId = jsonObject.getString(ScheduleParamEnum.SCHEDULE_PARAM_PROJECT.getCode());
      String year = jsonObject.getString(ScheduleParamEnum.SCHEDULE_PARAM_YEAR.getCode());
      String month = jsonObject.getString(ScheduleParamEnum.SCHEDULE_PARAM_MONTH.getCode());
      int monthInt = Integer.parseInt(month);
      accountCheckService.executeAccountCheckHandler(projectId, year, monthInt, monthInt);
    } catch (Exception e) {
      logger.error("执行内部对账任务{}失败", jobName, e);
    }
  }

}
